//
//  TopViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/12.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "TopViewController.h"
#import "BaseModel.h"
#import "DiscountModel.h"
#import "TopModel.h"
#import "TopTableViewCell.h"
#import "WebViewController.h"
#import "DiscountCollectionViewCell.h"
#import "LookSleeveViewController.h"
#import "DiscountViewController.h"
#import "PubViewController.h"
#import "JourneyViewController.h"
#import "AppDelegate.h"
#import "SDCycleScrollView.h"

// 4.签协议
@interface TopViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *picArr;
@property (nonatomic, strong) NSMutableArray *nextOneArr;
@property (nonatomic, strong) NSMutableArray *discountHeadArr;
@property (nonatomic, strong) NSMutableArray *discountArr;
@property (nonatomic, strong) NSMutableArray *hotArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIImageView *nextUpImageView;
@property (nonatomic, strong) UIImageView *nextLeftImageView;
@property (nonatomic, strong) UIImageView *nextRightImageView;
@property (nonatomic, strong) UITapGestureRecognizer *tapUp;
@property (nonatomic, strong) UITapGestureRecognizer *tapLeft;
@property (nonatomic, strong) UITapGestureRecognizer *tapRight;
@property (nonatomic, strong) UILabel *discountLabel;
@property (nonatomic, strong) UILabel *nextOneLabel;
@property (nonatomic, strong) UIImageView *discountImageView;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *discountButton;
@property (nonatomic, strong) UIButton *sleeveButton;
@property (nonatomic, strong) UILabel *sleeveLabel;
@property (nonatomic, strong) UIButton *knockDiscountButton;
@property (nonatomic, strong) UILabel *knockDiscountLabel;
@property (nonatomic, strong) UIButton *pubButton;
@property (nonatomic, strong) UILabel *pubLabel;
@property (nonatomic, strong) UIButton *journeyButton;
@property (nonatomic, strong) UILabel *journeyLabel;
@property (nonatomic, strong) NSMutableArray *rollPicArr;
@property (nonatomic, strong) SDCycleScrollView *picView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation TopViewController

// 视图的出现将tabbar显示
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
        
        self.hotArr = [NSMutableArray array];
        [self createHeadData];
        [self createHotData];
    }
    return self;
}
#pragma mark 头视图相关的数据解析
- (void)createHeadData {
    NSString *strUrl = @"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88265377636094&lon=121.5397673368396&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%25208.1.2&v=1%20";
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
              
        NSMutableDictionary *strDic = result;
        NSMutableDictionary *secDic = strDic[@"data"];
        
        // 轮播图数据解析
        NSMutableArray *rollArr = secDic[@"slide"];
        self.picArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in rollArr) {
            BaseModel *rollPicModel = [[BaseModel alloc] init];
            [rollPicModel setValuesForKeysWithDictionary:dic];
            [self.picArr addObject:rollPicModel];
        }
        
        self.rollPicArr = [NSMutableArray array];
        for (BaseModel *picModel in self.picArr) {
            [self.rollPicArr addObject:picModel.photo];
        }
        [self createRollPic];
        
        // 发现下一站
        NSMutableArray *nextOneArr = secDic[@"subject"];
        self.nextOneArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in nextOneArr) {
            BaseModel *nextOneModel = [[BaseModel alloc] init];
            [nextOneModel setValuesForKeysWithDictionary:dic];
            [self.nextOneArr addObject:nextOneModel];
        }
        [self crateImageData];
        
        // 抢折扣头视图
        NSMutableArray *discountHeadArr = secDic[@"discount_subject"];
        NSMutableDictionary *discountHeadDic = discountHeadArr[0];
        self.discountHeadArr = [NSMutableArray array];
        BaseModel *discountHeadModel = [[BaseModel alloc] init];
        [discountHeadModel setValuesForKeysWithDictionary:discountHeadDic];
        [self.discountHeadArr addObject:discountHeadModel];
        [self createDiscountHeadView];
        
        // 抢折扣
        NSMutableArray *discountArr = secDic[@"discount"];
        self.discountArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in discountArr) {
            DiscountModel *discountModel = [[DiscountModel alloc] init];
            [discountModel setValuesForKeysWithDictionary:dic];
            [self.discountArr addObject:discountModel];
        }
        [self.collectionView reloadData];
        
        [self.tableView reloadData];
        
        [self.tableView.header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"错误1是:%@",error);        
    }];
}

#pragma mark 看热门游记数据解析
- (void)createHotData {
    self.page++;
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/recommands/trip?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&lat=38.88260634985181&lon=121.5398368146665&page=%ld&track_app_channel=App%%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&type=index&v=1%%20",self.page];
    strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
              
        NSMutableDictionary *strDic = result;
        NSMutableArray *strArr = strDic[@"data"];
        for (NSMutableDictionary *dic in strArr) {
            TopModel *topModel = [[TopModel alloc] init];
            [topModel setValuesForKeysWithDictionary:dic];
            [self.hotArr addObject:topModel];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"错误2是%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"抽屉"] style:UIBarButtonItemStylePlain target:self action:@selector(presentRightMenuViewController:)];
    
    // 设置navigationBar颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"去哪旅行";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    [self createTableView];
}

#pragma mark tableView的创建
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 554)];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 90 * HEIGHT;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    [self createHeadView];
    [self createMJRefresh];
}

- (void)createMJRefresh {
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createHeadData)];
    [self.tableView.header beginRefreshing];
    
    // 上拉加载
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createHotData)];
}

#pragma mark 头视图的创建
- (void)createHeadView {
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 1150)];
    self.headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = self.headView;
    
    [self createNextOneView];
    [self createFourButtonView];
}

#pragma mark 轮播图
- (void)createRollPic {
    self.picView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 160) imageURLStringsGroup:self.rollPicArr];
    [self.headView addSubview:self.picView];
    
    self.picView.delegate = self;
}

// 轮播图的点击方法push到下一页
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    BaseModel *picModel = self.picArr[index];
    webVC.url = picModel.url;
}

#pragma mark 轮播图下面的4个button
- (void)createFourButtonView {
    // 看锦囊
    self.sleeveButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.sleeveButton.frame = CGRectMake(20 * WIDTH, 180 * HEIGHT, 70 * WIDTH, 70 * HEIGHT);
    [self.headView addSubview:self.sleeveButton];
    [self.sleeveButton setImage:[UIImage imageNamed:@"看锦囊"] forState:UIControlStateNormal];
    [self.sleeveButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sleeveLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * WIDTH, 240 * HEIGHT, 70 * WIDTH, 20 * HEIGHT)];
    self.sleeveLabel.text =@"看锦囊";
    [self.headView addSubview:self.sleeveLabel];
    self.sleeveLabel.font = [UIFont systemFontOfSize:15];
    
    // 抢折扣
    self.knockDiscountButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.knockDiscountButton.frame = CGRectMake(100 * WIDTH, 180 * HEIGHT, 70 * WIDTH, 70 * HEIGHT);
    [self.headView addSubview:self.knockDiscountButton];
    [self.knockDiscountButton setImage:[UIImage imageNamed:@"抢折扣"] forState:UIControlStateNormal];
    [self.knockDiscountButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.knockDiscountLabel = [[UILabel alloc] initWithFrame:CGRectMake(110 * WIDTH, 240 * HEIGHT, 70 * WIDTH, 20 * HEIGHT)];
    self.knockDiscountLabel.text =@"抢折扣";
    [self.headView addSubview:self.knockDiscountLabel];
    self.knockDiscountLabel.font = [UIFont systemFontOfSize:15];
    
    // 订酒店
    self.pubButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.pubButton.frame = CGRectMake(190 * WIDTH, 180 * HEIGHT, 70 * WIDTH, 70 * HEIGHT);
    [self.headView addSubview:self.pubButton];
    [self.pubButton setImage:[UIImage imageNamed:@"订酒店"] forState:UIControlStateNormal];
    [self.pubButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.pubLabel = [[UILabel alloc] initWithFrame:CGRectMake(200 * WIDTH, 240 * HEIGHT, 70 * WIDTH, 20 * HEIGHT)];
    self.pubLabel.text =@"订酒店";
    [self.headView addSubview:self.pubLabel];
    self.pubLabel.font = [UIFont systemFontOfSize:15];

    // 旅途中
    self.journeyButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.journeyButton.frame = CGRectMake(280 * WIDTH, 180 * HEIGHT, 70 * WIDTH, 70 * HEIGHT);
    [self.headView addSubview:self.journeyButton];
    [self.journeyButton setImage:[UIImage imageNamed:@"旅途中"] forState:UIControlStateNormal];
    [self.journeyButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.journeyLabel = [[UILabel alloc] initWithFrame:CGRectMake(290 * WIDTH, 240 * HEIGHT, 70 * WIDTH, 20 * HEIGHT)];
    self.journeyLabel.text =@"旅途中";
    [self.headView addSubview:self.journeyLabel];
    self.journeyLabel.font = [UIFont systemFontOfSize:15];
}
// 轮播图下面的4个button的点击方法
- (void)click:(UIButton *)button {
    if (button == self.sleeveButton) {
        LookSleeveViewController *lookSleeveVC = [[LookSleeveViewController alloc] init];
        [self.navigationController pushViewController:lookSleeveVC animated:YES];
    }
    else if (button == self.knockDiscountButton) {
        DiscountViewController *discountVC = [[DiscountViewController alloc] init];
        [self.navigationController pushViewController:discountVC animated:YES];
    }
    else if (button == self.pubButton) {
        PubViewController *pubVC = [[PubViewController alloc] init];
        [self.navigationController pushViewController:pubVC animated:YES];
    }
    else if (button == self.journeyButton) {
        JourneyViewController *journeyVC = [[JourneyViewController alloc] init];
        [self.navigationController pushViewController:journeyVC animated:YES];
    }
}

#pragma mark 发现下一站
- (void)createNextOneView {
    self.nextOneLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH, 288 * HEIGHT, 150 * WIDTH, 40 * HEIGHT)];
    self.nextOneLabel.text = @"发现下一站";
    self.nextOneLabel.font = [UIFont systemFontOfSize:17];
    [self.headView addSubview:self.nextOneLabel];
    
    self.nextUpImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 * WIDTH, 328 * HEIGHT, 365 * WIDTH, 150 * HEIGHT)];
    self.nextUpImageView.userInteractionEnabled = YES;
    [self.headView addSubview:self.nextUpImageView];
    // 添加手势
    self.tapUp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.nextUpImageView addGestureRecognizer:self.tapUp];
    
    self.nextLeftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 * WIDTH, 488 * HEIGHT, 180 * WIDTH, 115 * HEIGHT)];
    self.nextLeftImageView.userInteractionEnabled = YES;
    [self.headView addSubview:self.nextLeftImageView];
    // 添加手势
    self.tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.nextLeftImageView addGestureRecognizer:self.tapLeft];
    
    self.nextRightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(190 * WIDTH, 488 * HEIGHT, 180 * WIDTH, 115 * HEIGHT)];
    self.nextRightImageView.userInteractionEnabled = YES;
    [self.headView addSubview:self.nextRightImageView];
    // 添加手势
    self.tapRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.nextRightImageView addGestureRecognizer:self.tapRight];
}
// 图片的数据请求
- (void)crateImageData {
    [self.nextUpImageView sd_setImageWithURL:[NSURL URLWithString:((BaseModel *)self.nextOneArr[0]).photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
    [self.nextLeftImageView sd_setImageWithURL:[NSURL URLWithString:((BaseModel *)self.nextOneArr[1]).photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
    [self.nextRightImageView sd_setImageWithURL:[NSURL URLWithString:((BaseModel *)self.nextOneArr[2]).photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
}

#pragma mark 抢折扣头视图
- (void)createDiscountHeadView {
    self.discountLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH, 620 * HEIGHT, 150 * WIDTH, 40 * HEIGHT)];
    self.discountLabel.text = @"抢特价折扣";
    [self.headView addSubview:self.discountLabel];
    self.discountLabel.font = [UIFont systemFontOfSize:17];
    
    self.discountImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 * WIDTH, 660 * HEIGHT, 365 * WIDTH, 150 * HEIGHT)];
    [self.headView addSubview:self.discountImageView];
    [self.discountImageView sd_setImageWithURL:[NSURL URLWithString:((BaseModel *)self.discountHeadArr[0]).photo]placeholderImage:[UIImage imageNamed:@"占位图"]];
    self.discountImageView.userInteractionEnabled = YES;
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.discountImageView addGestureRecognizer:self.tap];
    
    [self createDiscountView];
}
// 手势点击push下一页
- (void)tapClick:(UITapGestureRecognizer *)tap {
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    if (tap == self.tapUp) {
        webVC.url = ((BaseModel *)self.nextOneArr[0]).url;
    }
    if (tap == self.tapLeft) {
        webVC.url = ((BaseModel *)self.nextOneArr[1]).url;
    }
    if (tap == self.tapRight) {
        webVC.url = ((BaseModel *)self.nextOneArr[2]).url;
    }
    if (tap == self.tap) {
        webVC.url = ((BaseModel *)self.discountHeadArr[0]).url;
    }
}

#pragma mark 抢折扣下面的collectionView
- (void)createDiscountView {
    self.discountButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.discountButton.frame = CGRectMake(125 * WIDTH, 1125 * HEIGHT, 150 * WIDTH, 20 * HEIGHT);
    [self.discountButton setTitle:@"查看全部>>>" forState:UIControlStateNormal];
    self.discountButton.alpha = 0.7;
    [self.discountButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.discountButton];
    
    // collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(180 * WIDTH, 130 * HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 30 *HEIGHT;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 815 * HEIGHT, WIDTH * 375, 300 * HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.headView addSubview:self.collectionView];
    [self.collectionView registerClass:[DiscountCollectionViewCell class] forCellWithReuseIdentifier:@"reauseHead"];

}
// 查看全部点击方法
- (void)buttonClick:(UIButton *)button {
    DiscountViewController *discountVC = [[DiscountViewController alloc] init];
    [self.navigationController pushViewController:discountVC animated:YES];
}

#pragma mark collectionView相关数据处理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.discountArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reauseHead" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    if (self.discountArr.count > indexPath.row) {
        DiscountModel *discountModel = self.discountArr[indexPath.row];
        [cell.upImageView sd_setImageWithURL:[NSURL URLWithString:discountModel.photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
        cell.mistLabel.text = discountModel.title;
        cell.downLeftLabel.text = discountModel.priceoff;
        NSString *strString = discountModel.price;
        if (strString.length == 15) {
            NSString *str = [strString substringWithRange:NSMakeRange(4, 4)];
            str = [NSString stringWithFormat:@"%@元起",str];
            cell.downRightLabel.text = str;
        }
        else if (strString.length == 16){
            
            NSString *str = [strString substringWithRange:NSMakeRange(4, 5)];
            str = [NSString stringWithFormat:@"%@元起",str];
            cell.downRightLabel.text = str;
        }
        else if (strString.length == 14) {
            NSString *str = [strString substringWithRange:NSMakeRange(4, 3)];
            str = [NSString stringWithFormat:@"%@元起",str];
            cell.downRightLabel.text = str;
        }

    }
    return cell;
}
    
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    DiscountModel *discountModel = self.discountArr[indexPath.row];
    // 需要拼接网址才能传值
    NSString *strUrl = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.5.5&track_user_id=6654242",discountModel.uId];
    webVC.url = strUrl;
}

#pragma mark tableView相关数据处理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hotArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"看热门游记";
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopModel *topModel = self.hotArr[indexPath.row];
    static NSString *reuse = @"reuse";
    TopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[TopTableViewCell alloc] initWithStyle:                (UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    if (self.hotArr.count > indexPath.row) {
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:topModel.photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
        cell.upLabel.text = topModel.title;
        cell.mistLabel.text = topModel.username;
        cell.downLeftImageView.image = [UIImage imageNamed:@"观看"];
        NSString *str = [NSString stringWithFormat:@"%@",topModel.views];
        cell.downLeftLabel.text = str;
        cell.downRightImageView.image = [UIImage imageNamed:@"评论"];
        cell.downRightLabel.text = topModel.replys;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TopModel *topModel = self.hotArr[indexPath.row];
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    webVC.url = topModel.view_url;
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        [self.discountButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.sleeveLabel.textColor = [UIColor whiteColor];
        self.knockDiscountLabel.textColor = [UIColor whiteColor];
        self.pubLabel.textColor = [UIColor whiteColor];
        self.journeyLabel.textColor = [UIColor whiteColor];
        self.nextOneLabel.textColor = [UIColor whiteColor];
        self.discountLabel.textColor = [UIColor whiteColor];
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self.discountButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.sleeveLabel.textColor = [UIColor blackColor];
        self.knockDiscountLabel.textColor = [UIColor blackColor];
        self.pubLabel.textColor = [UIColor blackColor];
        self.journeyLabel.textColor = [UIColor blackColor];
        self.nextOneLabel.textColor = [UIColor blackColor];
        self.discountLabel.textColor = [UIColor blackColor];
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
